using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class DocType : Page, IRequiresSessionState
{
   
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setButtons(string.Empty);
        this.setTextReadOnly(true);

        /******Modify by RP*********/
        ClearGroupType();
        /***************/
    }

    
  
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "DocType");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            try
            {   
                /******Modify by RP*********/   
                string strSection = ddlSection.SelectedValue.ToString();
                string strType = txtType.Text.Trim();
                /***************/

                this.ODSDocType.DeleteMethod = "Delete";
                this.ODSDocType.DeleteParameters["Original_Section"].DefaultValue = this.ddlSection.SelectedValue.ToString();
                this.ODSDocType.DeleteParameters["Original_Type"].DefaultValue = this.txtType.Text.ToString();
                this.ODSDocType.Delete();
                /******Modify by RP*********/  
                string sqlStr = "delete from DocumentTypeCode where Section='" + strSection + "' and Type='" + strType + "'";
                ClassLibrary.BusinessLayer.BusinessObjects.Db.Delete(sqlStr);
                /***************/


                this.clearText();
                this.setButtons(string.Empty);
                this.setTextReadOnly(true);
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.chkForOperation.Checked = true;
            }
        }

        /******Modify by RP*********/ 
        ClearGroupType();
        /***************/
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "DocType");
        if (!canDelete.CanUpdate())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoEdit.ToString();
        }
        else
        {
            this.setButtons("modify");
            this.setTextReadOnly(false);
            this.ddlSection.Enabled = false;
            this.txtType.ReadOnly = true;
            this.txtType.Enabled = false;
            this.txtTypeSeq.Focus();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "DocType");
        if (!canDelete.CanAdd())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoAdd.ToString();
        }
        else
        {
            this.clearText();
            this.setButtons("new");
            this.setTextReadOnly(false);
            this.ddlSection.Focus();            
        }
        /******Modify by RP*********/ 
        ClearGroupType();
        /***************/

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.cbForUpdate.Checked)
            {
                this.ODSDocType.UpdateMethod = "Update";
                this.ODSDocType.UpdateParameters["Section"].DefaultValue = this.ddlSection.SelectedValue.ToString();
                this.ODSDocType.UpdateParameters["Type"].DefaultValue = this.txtType.Text.ToString();
                this.ODSDocType.UpdateParameters["TypeSeq"].DefaultValue = this.txtTypeSeq.Text.ToString();
                this.ODSDocType.UpdateParameters["Folder"].DefaultValue = this.txtFolder.Text.ToString();
                this.ODSDocType.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSDocType.UpdateParameters["Original_Section"].DefaultValue = this.ddlSection.SelectedValue.ToString();
                this.ODSDocType.UpdateParameters["Original_Type"].DefaultValue = this.txtType.Text.ToString();
                this.ODSDocType.Update();
                              
            }
            else
            {
                this.ODSDocType.InsertMethod = "Insert";
                this.ODSDocType.InsertParameters["Section"].DefaultValue = this.ddlSection.SelectedValue.ToString();
                this.ODSDocType.InsertParameters["TypeSeq"].DefaultValue = this.txtTypeSeq.Text.ToString();
                this.ODSDocType.InsertParameters["Type"].DefaultValue = this.txtType.Text.ToString();
                this.ODSDocType.InsertParameters["Folder"].DefaultValue = this.txtFolder.Text.ToString();
                this.ODSDocType.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSDocType.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSDocType.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSDocType.Insert();
            }

            /******Modify by RP*********/

            string sqlStr = "select * from DocumentTypeCode where Section='" + this.ddlSection.SelectedValue.ToString() + "' and Type='" + txtType.Text.Trim() + "'";
            DataTable dt = ClassLibrary.BusinessLayer.BusinessObjects.Db.GetDataTable(sqlStr);

            if (dt != null && dt.Rows.Count > 0)
            {
                string sqlDel = "delete from DocumentTypeCode where Section='" + this.ddlSection.SelectedValue.ToString() + "' and Type='" + txtType.Text.Trim() + "'";
                ClassLibrary.BusinessLayer.BusinessObjects.Db.Delete(sqlDel);
            }
            foreach (RepeaterItem rptItem in rptGroupType.Items)
            {
                CheckBox cb = (CheckBox)rptItem.FindControl("chkGroupTP");
                if (cb.Checked)
                {
                    string sqlInsert = "insert into DocumentTypeCode(Section,Type,GroupTypeCode) values('" + this.ddlSection.SelectedValue.Trim() + "','" + txtType.Text.Trim() + "','" + cb.Text.Trim() + "')";
                    ClassLibrary.BusinessLayer.BusinessObjects.Db.Insert(sqlInsert);
                }
            }
            /***************/


            this.clearText();
            this.setButtons(string.Empty);
            this.setTextReadOnly(true);
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            this.chkForOperation.Checked = true;
        }
    }

    protected void clearText()
    {
        this.ddlSection.SelectedIndex = -1;
        this.txtTypeSeq.Text = string.Empty;
        this.txtType.Text = string.Empty;
        this.txtFolder.Text = string.Empty;
        this.cbForUpdate.Checked = false;
        this.chkForOperation.Checked = false;
        this.lblError.Text = "";
    }

    protected void ddlSection_PreRender(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString()))
                {
                    this.ddlSection.SelectedIndex = -1;
                    this.ddlSection.Items.FindByValue(this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString()).Selected = true;
                }
            }
            catch (ArgumentOutOfRangeException)
            {
            }
        }
    }

    /******Modify by RP*********/
    private void FillDDl()
    {
        IPTableAdapters.DocumentSectionTableAdapter ct = new IPTableAdapters.DocumentSectionTableAdapter();
        IP.DocumentSectionDataTable dt = ct.GetDocSectionData();
        ddlSection.DataSource = dt;
        ddlSection.DataTextField = "Description";
        ddlSection.DataValueField = "Section";
        ddlSection.DataBind();
        ddlSection.Items.Insert(0, new ListItem("", ""));
    }
    /***************/


    protected void gvDocType_RowCreated(object sender, GridViewRowEventArgs e)
    {
        this.Sort(e, this.gvDocType);
    }

    /******Modify by RP*********/
    private void ClearGroupType()
    {
        foreach (RepeaterItem rptItem in rptGroupType.Items)
        {
            CheckBox cb = (CheckBox)rptItem.FindControl("chkGroupTP");
            cb.Checked = false;
        }
    }
    /***************/

   
   
    protected void gvDocType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            /******Modify by RP*********/
            ClearGroupType();
            /***************/
            this.chkForOperation.Checked = true;
            this.cbForUpdate.Checked = true;
            DocumentType runMethod = new DocumentType(null, null, null);
            runMethod.GetDocumentTypeInfo(this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString(), this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["TypeSeq"].ToString());
            this.txtTypeSeq.Text = this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["TypeSeq"].ToString();
            this.txtType.Text = runMethod.type;
            this.txtFolder.Text = runMethod.folder;
            // hidSession.Value = this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString();
            ddlSection.SelectedValue = this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString();

            string sqlStr = "select * from DocumentTypeCode where Section='" + this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Section"].ToString() + "' and Type='" + this.gvDocType.DataKeys[this.gvDocType.SelectedIndex].Values["Type"].ToString() + "'";
            DataTable dt = ClassLibrary.BusinessLayer.BusinessObjects.Db.GetDataTable(sqlStr);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    foreach (RepeaterItem rptItem in rptGroupType.Items)
                    {
                        CheckBox cb = (CheckBox)rptItem.FindControl("chkGroupTP");
                        if (dt.Rows[i]["GroupTypeCode"].ToString() == cb.Text.Trim())
                        {
                            cb.Checked = true;
                            break;
                        }                       
                    }
                }
            }

        }
        catch (Exception)
        {
        }
        this.setButtons(string.Empty);
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "DocType");
        if (!canView.CanView())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            this.setButtons(string.Empty);
            this.setTextReadOnly(true);
            if (!this.Page.IsPostBack)
            {
                /******Modify by RP*********/
               
                FillDDl();
                IPTableAdapters.CodeTableAdapter codeAd = new IPTableAdapters.CodeTableAdapter();
                IP.CodeDataTable dt = codeAd.GetCodesByType("PT");
                rptGroupType.DataSource = dt;
                rptGroupType.DataBind();
                ClearGroupType();
                ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.DocumentSetup + " | " + ISitemap.DocumentType;
                /***************/
                this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
            }
        }
    }

    public void setButtons(string action)
    {
        switch (action)
        {
            case "new":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;

            case "modify":
                this.btnNew.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                this.btnDelete.Enabled = false;
                return;
        }
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtTypeSeq.Text.Length != 0;
        this.btnSave.Enabled = false;
        this.btnDelete.Enabled = this.txtTypeSeq.Text.Length != 0;
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.ddlSection.Enabled = !bBool;
        this.txtTypeSeq.ReadOnly = bBool;
        this.txtType.ReadOnly = bBool;
        this.txtFolder.ReadOnly = bBool;
    }

    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }
    /******Modify by RP*********/
    
    protected void rptGroupType_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       
    }
    /***************/
}
